// Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
//  
// This file is part of SrtSync Project. See http://code.google.com/p/srtsync/ for more information.
//  
// SrtSync is an open source project. Using of SrtSync is free of charge.
// It is distributed under GNU General Public License version 3 (GPLv3).
// GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
//  
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.

using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace SrtSync.Core
{
    public class PersistentEncoding
    {
        private const int DefaultEncodingCodePage = 1250;
        private Encoding _encoding;

        [XmlIgnore]
        public Encoding Value
        {
            get
            {
                if (_encoding == null)
                {
                    EncodingInfo ei = Encoding.GetEncodings().First(x => x.CodePage == DefaultEncodingCodePage);

                    _encoding = ei.GetEncoding();
                }

                return _encoding;
            }

            set
            {
                _encoding = value;
            }
        }

        public int CodePage
        {
            get
            {
                return Value.CodePage;
            }

            set
            {
                EncodingInfo e = Encoding.GetEncodings().FirstOrDefault(x => x.CodePage == value);
                if (e != null)
                {
                    Value = e.GetEncoding();
                }
            }
        }
    }
}